package com.integralPerformance.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.integralPerformance.common.Result;
import com.integralPerformance.exception.ServiceNameException;
import com.integralPerformance.exception.TeacherNameException;
import com.integralPerformance.pojo.entity.ServiceInfo;
import com.integralPerformance.pojo.excel.ServiceInfoExcel;
import com.integralPerformance.pojo.form.add.AddServiceInfo;
import com.integralPerformance.pojo.form.update.UpdateServiceInfo;
import com.integralPerformance.pojo.query.GetMyServiceInfoQuery;
import com.integralPerformance.pojo.query.ServiceInfoQuery;
import com.integralPerformance.pojo.vo.BaseVo;
import com.integralPerformance.pojo.vo.ExportServiceInfoVo;
import com.integralPerformance.pojo.vo.ServiceInfoByNameAndYearVo;
import com.integralPerformance.pojo.vo.ServiceInfoVo;
import com.integralPerformance.service.ServiceInfoService;
import com.integralPerformance.mapper.ServiceInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author admin
* @description 针对表【service_info(公共服务信息维护)】的数据库操作Service实现
* @createDate 2024-03-14 17:04:57
*/
@Service
public class ServiceInfoServiceImpl extends ServiceImpl<ServiceInfoMapper, ServiceInfo>
    implements ServiceInfoService{

    @Autowired
    ServiceInfoMapper serviceInfoMapper;


    public String getTeacherId(String teacherName){
        return serviceInfoMapper.validTeacherName(teacherName);
    }

    public String getServiceBaseId(String serviceName){
        return String.valueOf(serviceInfoMapper.validServiceName(serviceName));
    }


    // 验证教师姓名是否合理。
    private String validTeacherName(String teacherName){
        String teacherId = serviceInfoMapper.validTeacherName(teacherName);
        if (teacherId==null){
            throw new ServiceNameException();
        }
        return teacherId;
    }

    // 验证业绩类型是否合理。
    private Integer validServiceName(String serviceName){
        Integer integer = serviceInfoMapper.validServiceName(serviceName);
        if (integer==null){
            throw new TeacherNameException();
        }
        return integer;
    }


    @Override
    public Result<BaseVo<ServiceInfoVo>> getServiceInfo(ServiceInfoQuery serviceInfoQuery) {

        Page<ServiceInfoVo> page = new Page<>(serviceInfoQuery.getCurrentPage(), serviceInfoQuery.getPageSize());
        Page<ServiceInfoVo> result = serviceInfoMapper.getServiceInfo(page, serviceInfoQuery);

        BaseVo<ServiceInfoVo> criteriaBaseVo = new BaseVo<>();
        criteriaBaseVo.setCurrentPage(serviceInfoQuery.getCurrentPage());
        criteriaBaseVo.setPageSize(serviceInfoQuery.getPageSize());
        criteriaBaseVo.setAllPages(result.getPages());
        criteriaBaseVo.setAllRecordNum(result.getTotal());
        criteriaBaseVo.setRecords(result.getRecords());

        return Result.success(criteriaBaseVo);

    }

    @Override
    public Result<BaseVo<ServiceInfoByNameAndYearVo>> getServiceInfoByTree(ServiceInfoQuery serviceInfoQuery) {
        Page<ServiceInfoByNameAndYearVo> voPage = new Page<>(serviceInfoQuery.getCurrentPage(), serviceInfoQuery.getPageSize());
        Page<ServiceInfoByNameAndYearVo> yearVoPage = serviceInfoMapper.getPageServiceInfoByNameAndYear(voPage,serviceInfoQuery);

        List<ServiceInfoByNameAndYearVo> records = yearVoPage.getRecords();
        for (int i = 0; i < records.size(); i++) {
            String teacherName = records.get(i).getTeacherName();
            String year = records.get(i).getYear();
            records.get(i).setServiceInfoVo(serviceInfoMapper.getServiceInfoByNameAndYear(teacherName,year,serviceInfoQuery));
        }

        BaseVo<ServiceInfoByNameAndYearVo> criteriaBaseVo = new BaseVo<>();
        criteriaBaseVo.setCurrentPage(serviceInfoQuery.getCurrentPage());
        criteriaBaseVo.setPageSize(serviceInfoQuery.getPageSize());
        criteriaBaseVo.setAllPages(yearVoPage.getPages());
        criteriaBaseVo.setAllRecordNum(yearVoPage.getTotal());
        criteriaBaseVo.setRecords(yearVoPage.getRecords());

        return Result.success(criteriaBaseVo);
    }

    @Override
    public Result<String> addServiceInfo(AddServiceInfo addServiceInfo) {
        Integer id = validServiceName(addServiceInfo.getServiceBaseName());
        String teacherId = validTeacherName(addServiceInfo.getTeacherName());
        ServiceInfo serviceInfo = new ServiceInfo();

        serviceInfo.setTeacherId(teacherId);
        serviceInfo.setServiceBaseId(String.valueOf(id));
        serviceInfo.setNote(addServiceInfo.getNote());
        serviceInfo.setReporter(addServiceInfo.getReporter());
        serviceInfo.setServerLength(addServiceInfo.getServerLength());
        serviceInfo.setYear(addServiceInfo.getYear());
        serviceInfoMapper.insert(serviceInfo);
        return Result.success("添加成功");
    }

    @Override
    public Result<String> deleteServiceInfoById(String id) {
        serviceInfoMapper.deleteById(id);
        return Result.success("删除成功");
    }

    @Override
    public Result<String> deleteServiceInfoByIds(List<Long> ids) {
        serviceInfoMapper.deleteBatchIds(ids);
        return Result.success("删除成功");
    }

    @Override
    public Result<String> updateServiceInfo(UpdateServiceInfo updateServiceInfo) {
        Integer id = validServiceName(updateServiceInfo.getServiceBaseName());
        String teacherId = validTeacherName(updateServiceInfo.getTeacherName());
        ServiceInfo serviceInfo = new ServiceInfo();

        serviceInfo.setId(updateServiceInfo.getId());
        serviceInfo.setTeacherId(teacherId);
        serviceInfo.setServiceBaseId(String.valueOf(id));
        serviceInfo.setNote(updateServiceInfo.getNote());
        serviceInfo.setReporter(updateServiceInfo.getReporter());
        serviceInfo.setServerLength(updateServiceInfo.getServerLength());
        serviceInfo.setYear(updateServiceInfo.getYear());
        serviceInfoMapper.updateById(serviceInfo);

        return Result.success("修改成功");

    }

    @Override
    public Result<BaseVo<ServiceInfoVo>> getServiceInfoById(GetMyServiceInfoQuery getMyServiceInfoQuery) {
        Page<ServiceInfoVo> page = new Page<>(getMyServiceInfoQuery.getCurrentPage(), getMyServiceInfoQuery.getPageSize());
        Page<ServiceInfoVo> result = serviceInfoMapper.getServiceInfoById(page, getMyServiceInfoQuery);

        BaseVo<ServiceInfoVo> criteriaBaseVo = new BaseVo<>();
        criteriaBaseVo.setCurrentPage(getMyServiceInfoQuery.getCurrentPage());
        criteriaBaseVo.setPageSize(getMyServiceInfoQuery.getPageSize());
        criteriaBaseVo.setAllPages(result.getPages());
        criteriaBaseVo.setAllRecordNum(result.getTotal());
        criteriaBaseVo.setRecords(result.getRecords());

        return Result.success(criteriaBaseVo);
    }

    @Override
    public Result<List<ServiceInfoVo>> getServiceInfoByTeacherId(String teacherId){
        List<ServiceInfoVo> serviceInfoVoList =serviceInfoMapper.getServiceInfoByTeacherId(teacherId);
        return Result.success(serviceInfoVoList);
    }

    @Override
    public boolean judge(ServiceInfoExcel serviceInfoExcel) {
        return serviceInfoMapper.judge(serviceInfoExcel);
    }

    @Override
    public List<Integer> saveList(List<ServiceInfoExcel> cachedDataList) {
        return serviceInfoMapper.saveList(cachedDataList);
    }

    @Override
    public List<ServiceInfoVo> getSaveDataIdList(List<Integer> saveDataIdList) {
        return serviceInfoMapper.getSaveDataIdList(saveDataIdList);
    }

    @Override
    public List<ExportServiceInfoVo> downloadByBatch(ServiceInfoQuery serviceInfoQuery) {
        return serviceInfoMapper.downloadByBatch(serviceInfoQuery);
    }




}




